package com.mingqijia.gassafety.webserver.service.cmp.impl;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mingqijia.gassafety.db.entity.*;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.constant.EquipmentType;
import com.mingqijia.gassafety.webserver.dto.ReportNotifyDTO;
import com.mingqijia.gassafety.webserver.dto.ReportNotifyOfflineDTO;
import com.mingqijia.gassafety.webserver.request.cmp.notify.DataReportDataRequest;
import com.mingqijia.gassafety.webserver.request.cmp.notify.DataReportRequest;
import com.mingqijia.gassafety.webserver.service.ReceiverValveService;
import com.mingqijia.gassafety.webserver.service.RecordReceiverValveService;
import com.mingqijia.gassafety.webserver.service.cmp.AbstractCmpDataNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import static com.mingqijia.gassafety.webserver.constant.DeviceConstant.*;

/**
 *  CMP平台运行上报回调具体实现
 *
 * @author zhufeng
 * @since 2022/8/4
 */
@Service
@Slf4j
public class CmpRunningNotifyServiceImpl extends AbstractCmpDataNotifyService {

    @Autowired
    ReceiverValveService receiverValveService;
    @Autowired
    RecordReceiverValveService recordReceiverValveService;

    @Override
    protected DeviceState prepareDeviceState(DataReportRequest request, Dtu dtu) {
        DeviceState deviceState = new DeviceState();
        deviceState.setDeviceId(dtu.getId());
        deviceState.setDetectorCount(request.getDetectorNum());
        // (家报,一体)设备由探测器部分统一决定总状态, 控制器单独决定
        if (EquipmentType.GAS_ALARM_CONTROLLER.getCode().equals(dtu.getEquipmentType())){
            setStatusInfo(request.getControllerStatus(), null, deviceState );
        }
        deviceState.setSpId(dtu.getSpId());
        return deviceState;
    }

    @Override
    protected RecordDetector prepareRecordDetector(DataReportDataRequest t, Dtu newDtu) {
        RecordDetector recordDetector = new RecordDetector();
        recordDetector.setDeviceId(newDtu.getId());
        recordDetector.setSubId(t.getSno());
        recordDetector.setRecordId(t.getRecordId());
        recordDetector.setConsumerId(newDtu.getConsumerId());
        recordDetector.setDetectorStatus(t.getDetectorStatus());
        recordDetector.setRecordAt(t.getRecordAt());
        recordDetector.setSpId(t.getSpId());
        return recordDetector;
    }

    @Override
    protected void saveReceiverValve(DataReportRequest request, Dtu dtu, Long controllerId, ReportNotifyDTO notifyDTO, ReportNotifyOfflineDTO offlineDTO) {
        log.info("运行上报的电磁阀信息记录");
        ReceiverValve newReceiverValve = new ReceiverValve();
        LambdaQueryWrapper<ReceiverValve> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReceiverValve::getDeviceId,dtu.getIMEI());
        ReceiverValve receiverValve = receiverValveService.getOne(queryWrapper);
        //没有接收器和电磁阀并且上报状态是离线，则不做任何操作
        if (VALVE_CONNECTION_0.equals(request.getValveConnection()) && (receiverValve == null || receiverValve.getIsDeleted().equals(1))) {
            log.info("没有接收器和电磁阀并且上报状态是离线，则不做任何操作");
            return;
        } else {
            newReceiverValve.setDeviceId(dtu.getIMEI());
            if (VALVE_CONNECTION_0.equals(request.getValveConnection())) {
                newReceiverValve.setReceiverStatus(RECEIVER_STATUS_OFFLINE);
            } else {
                newReceiverValve.setReceiverStatus(RECEIVER_STATUS_NORMAL);
                if (request.getValveConnection() > 1) {
                    newReceiverValve.setValveId(String.valueOf(request.getValveConnection()));
                }
            }
            //故障
            String valveStatusDesc = request.getValveStatusDesc() == null ? "":request.getValveStatusDesc();
            //接收器和电磁阀离线
            if (valveStatusDesc.contains(VALVE_BROKEN_STATUS_71.toString())) {
                newReceiverValve.setReceiverStatus(RECEIVER_STATUS_OFFLINE);
            }
            if (VALVE_WORK_STATUS_1.equals(request.getValveWorkStatus())) {
                newReceiverValve.setBrokenStatus(VALVE_WORK_STATUS_BROKEN);
                if (!StringUtils.isEmpty(valveStatusDesc)) {
                    //电池欠压
                    if (valveStatusDesc.contains(VALVE_BROKEN_STATUS_74.toString())) {
                        newReceiverValve.setBatteryStatus(BATTERY_STATUS_BROKEN);
                    }
                    String brokenStatusInfo = "";
                    if (valveStatusDesc.contains(VALVE_BROKEN_STATUS_70.toString())) {
                        brokenStatusInfo = VALVE_BROKEN_STATUS_70_INFO+";";
                    }
                    if (valveStatusDesc.contains(VALVE_BROKEN_STATUS_71.toString())) {
                        brokenStatusInfo = brokenStatusInfo + VALVE_BROKEN_STATUS_71_INFO+";";
                    }
                    if (valveStatusDesc.contains(VALVE_BROKEN_STATUS_72.toString())) {
                        brokenStatusInfo = brokenStatusInfo + VALVE_BROKEN_STATUS_72_INFO+";";
                    }
                    if (valveStatusDesc.contains(VALVE_BROKEN_STATUS_73.toString())) {
                        brokenStatusInfo = brokenStatusInfo + VALVE_BROKEN_STATUS_73_INFO+";";
                    }
                    newReceiverValve.setBrokenStatusInfo(brokenStatusInfo);
                }
            } else {
                newReceiverValve.setBrokenStatus(VALVE_WORK_STATUS_NORMAL);
                newReceiverValve.setBrokenStatusInfo("");
            }
            if (VALVE_STATUS_0.equals(request.getValveStatus())) {
                newReceiverValve.setValveStatus(VALVE_STATUS_OPEN);
            } else if(VALVE_STATUS_1.equals(request.getValveStatus())){
                newReceiverValve.setValveStatus(VALVE_STATUS_CLOSE);
            }
            newReceiverValve.setSpId(dtu.getSpId());
            if (receiverValve == null) {
                log.info("新增接收器和电磁阀状态更新表："+newReceiverValve.toString());
                receiverValveService.save(newReceiverValve);
            } else {
                if (!valveStatusDesc.contains(VALVE_BROKEN_STATUS_74.toString())) {
                    newReceiverValve.setBatteryStatus(BATTERY_STATUS_NORMAL);
                }
                newReceiverValve.setIsDeleted(0);
                newReceiverValve.setId(receiverValve.getId());
                if (receiverValve.getIsDeleted().equals(1)) {
                    newReceiverValve.setReceiverCode("");
                    newReceiverValve.setReceiverType("");
                    newReceiverValve.setReceiverPowerMode("");
                }
                log.info("更新接收器和电磁阀状态更新表："+newReceiverValve.toString());
                receiverValveService.updateById(newReceiverValve);
            }
            if (notifyDTO !=null) {
                notifyDTO.setReceiverValve(receiverValve);
                notifyDTO.setPrepareReceiverValve(newReceiverValve);
            }
            if (offlineDTO != null) {
                offlineDTO.setReceiverValve(newReceiverValve);
            }
        }
        RecordReceiverValve recordReceiverValve = new RecordReceiverValve();
        recordReceiverValve.setRecordId(controllerId);
        recordReceiverValve.setDeviceId(dtu.getIMEI());
        if (VALVE_CONNECTION_0.equals(request.getValveConnection())) {
            recordReceiverValve.setReceiverStatus(RECEIVER_STATUS_OFFLINE);
        } else {
            recordReceiverValve.setReceiverStatus(RECEIVER_STATUS_NORMAL);
            if (request.getValveConnection() > 1) {
                recordReceiverValve.setValveId(String.valueOf(request.getValveConnection()));
            }
        }
        recordReceiverValve.setValveWorkStatus(request.getValveWorkStatus());
        recordReceiverValve.setValveStatus(request.getValveStatus());
        recordReceiverValve.setBrokenStatus(request.getValveStatusDesc());
        recordReceiverValve.setRecordAt(new Date());
        recordReceiverValve.setSpId(dtu.getSpId());
        log.info("新增接收器和电磁阀上报数据："+recordReceiverValve.toString());
        recordReceiverValveService.save(recordReceiverValve);
    }

    @Override
    protected DeviceState updateDeviceStateByDetector(DataReportDataRequest t, Dtu newDtu, Integer controllerStatus) {
        DeviceState deviceState = new DeviceState();
        deviceState.setDeviceId(newDtu.getId());
        //本次上报的 控制器状态和探测器状态共同决定 设备状态
        //当修改探测器时, controllerStatus = null
        DeviceState state = getStateByDeviceId(newDtu.getId(), newDtu.getSpId()); //该探头当前状态
        if (state !=null){
            // 当设备为报警时, 不应去更新设备状态,须由监测上报更新（紫荆WiFi报警器除外）
            if (!(DEVICE_STATUS_2.equals(state.getDeviceStatus()) || DEVICE_STATUS_3.equals(state.getDeviceStatus()) ||
                    DEVICE_STATUS_6.equals(state.getDeviceStatus())) || Constants.ZJ_EQUIPMENT_FROM.equals(newDtu.getEquipmentFrom())) {
                log.info("设备:{} 此前状态不为报警（紫荆WiFi报警器除外）, 进行状态更新", newDtu.getId());
                setStatusInfo(controllerStatus, t.getDetectorStatus(), deviceState);
            }else {
                log.info("设备:{} 此前状态为报警, 不进行状态更新", newDtu.getId());
            }
        }else {
            setStatusInfo(controllerStatus, t.getDetectorStatus(), deviceState);
        }
        deviceState.setSpId(t.getSpId());
        return deviceState;
    }

    @Override
    protected RecordController prepareRecordController(DataReportRequest dataReportRequest, Dtu dtu) {
        RecordController recordController = new RecordController();
        recordController.setDeviceId(dtu.getId());
        recordController.setConsumerId(dtu.getConsumerId());
        recordController.setRecordType(dataReportRequest.getRecordType());
        recordController.setRecordAt(dataReportRequest.getReportDate());
        recordController.setWorkStatus(dataReportRequest.getWorkStatus());
        recordController.setControllerStatus(dataReportRequest.getControllerStatus());
        recordController.setDetectorCount(dataReportRequest.getDetectorNum());
        recordController.setSpId(dtu.getSpId());
        return recordController;
    }

}
